<?php
/*
* @Author: hzwlxy
* @Email: 120235331@qq.com
* @Github: http：//www.github.com/siaoynli
* @Date: 2019/6/27 16:50
* @Version:
* @Description:
*/

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:18
 * @Description:测试helper
 */
function ping(){
    echo "pong";
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/7/3 16:08
 * @Description: ajax 返回json
 * @param $message
 * @param int $state
 * @param string $url
 * @return \Illuminate\Http\JsonResponse
 */
function  responseJsonData($message,$state=0,$url=""){
    return response()->json(["message"=>$message,"state"=>$state,"url"=>$url,"code"=>0]);
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/7/3 16:12
 * @Description: 异常返回json 针对layui框架
 * @param $message
 * @param int $code
 * @return \Illuminate\Http\JsonResponse
 */
function  responseExceptionJsonData($message,$code=1){
    return response()->json(["message"=>$message,"code"=>$code]);
}


/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 16:56
 * @Description: 过滤用户提交的数据
 * @param null $data
 * @param array $fields  忽略的字段
 * @return array
 */
function filterRequest($data = null, array $fields = [])
{
    $store = $data ?: request()->all();
    foreach ($store as $key => $value) {
        if (in_array($key, $fields)) {
            continue;
        }
        if (!is_array($value)) {
            $store[$key] = e(trim($value));
        } else {
            $store[$key] = filterRequest($value, $fields);
        }
    }
    return $store;
}


/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 16:58
 * @Description: 删除public和storage/app目录下的文件
 * @param $file
 */
function delFile($file)
{
    if (strpos($file, "http") !== 0) {
        $_filename = public_path($file);
        if (file_exists($_filename)) {
            @unlink($_filename);
        }
        $_filename = storage_path('app'.$file);
        if (file_exists($_filename)) {
            @unlink($_filename);
        }
    }
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 16:59
 * @Description: 删除目录下所有文件
 * @param $dir
 * @return mixed
 */
function delDirAndFile($dir)
{
    if (!!$handle = opendir($dir)) {
        while (false !== ($item = readdir($handle))) {
            if ($item != "." && $item != "..") {
                if (is_dir("$dir/$item")) {
                    delDirAndFile("$dir/$item");
                } else {
                    if (!unlink("$dir/$item")) {
                        return $dir;
                    }
                }
            }
        }
    }
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:00
 * @Description: 根据空格分隔字符串
 * @param $str
 * @return array[]|false|string[]
 */
function explodeStringByBlank($str)
{
    return preg_split('/[\s]+/', $str);
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:02
 * @Description:根据空格，逗号，点分隔字符串
 * @param $str
 * @return array[]|false|string[]
 */
function explodeStringByBlankAndComma($str)
{
    return preg_split('/([\s]+|,|、|，)/is', $str);
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:05
 * @Description: 将多个空格，逗号，点替换成空格 或对应字符串
 * @param $str
 * @return string|string[]|null
 */
function replaceBlankOrCommaToBlank($str,$replacement=" ")
{
    return preg_replace('/([\s]+|,|、|，)/is', $replacement, $str);
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:06
 * @Description: 上传图片增加small路径
 * @param $imgfile
 * @return string
 */
function setSmallImgPath($imgfile)
{
    $dir = dirname($imgfile);
    $filename = basename($imgfile);
    return $dir . '/small/' . $filename;
}


/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:09
 * @Description: 图片地址增加hash值
 * @param $imgfile
 * @return string
 */
function setOSSImgPath($imgfile)
{
    $dir = dirname($imgfile);
    $filename = basename($imgfile);
    return $dir . '/' . sha1(time()) . '_' . $filename;
}

/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:08
 * @Description:url连接状态
 * @param $url
 * @return bool
 */
function isHttpUrlOk($url)
{
    $header_array = get_headers($url, true);
    if ($header_array[0] == 'HTTP/1.1 200 OK') {
        return true;
    } else {
        return false;
    }
}


/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:11
 * @Description: oss文件是否存在
 * @param $file
 * @return bool
 */
function isOssFileExit($file)
{
    return isHttpUrlOk(env('OSS_URL') . '/' . $file);
}


/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:12
 * @Description:  图片转base64编码
 * @param $image_file
 * @return string
 */
function base64EncodeImage($image_file,$is_public=false)
{
    $image_file = $is_public?public_path($image_file):$image_file;
    $image_info = getimagesize($image_file);
    $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
    $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
    return $base64_image;
}


/**
 * @Author: hzwlxy
 * @Email: 120235331@qq.com
 * @Date: 2019/6/27 17:14
 * @Description:数据加密解密
 * @param $string
 * @param string $operation
 * @param string $key
 * @param int $expiry
 * @return string
 */
function sys_auth($string, $operation = 'ENCODE', $key = '', $expiry = 0)
{
    $string = trim($string);
    if ($operation == 'ENCODE') {
        $string = urlencode($string);
    }

    $key_length = 4;
    $key = md5($key != '' ? $key : env('APP_KEY'));
    $fixedkey = md5($key);
    $egiskeys = md5(substr($fixedkey, 16, 16));
    $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
    $keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
    $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));

    $i = 0;
    $result = '';
    $string_length = strlen($string);
    for ($i = 0; $i < $string_length; $i++) {
        $result .= chr(ord($string{$i}) ^ ord($keys{$i % 32}));
    }
    if ($operation == 'ENCODE') {
        return $runtokey . base64_encode($result);
    } else {
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $egiskeys), 0, 16)) {
            return urldecode(substr($result, 26));
        } else {
            return '';
        }
    }
}